# -*- coding: utf-8 -*-
# 快恋网搜索结果爬虫
import json
from datetime import datetime

import xlwt

from klSpiderDetail import mainDetail


def main(current, size):
    baseUrl = "http://www.kl.cc/SearchLuck/GetSearchJson?t=Sat%20Jun%2006%202020%2017:46:36%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)"
    # 爬取数据
    dataList = getDataList(baseUrl, current, size)
    # 循环VIPid--得到每一个主页--对象对应一个主页数据，集合包含多条主页数据写入-个人主页保存
    saveDetailPath = "快恋搜索详细结果数据-第" + str(current) + "页开始" + str(datetime.today().date()) + ".xls"
    mainDetail(dataList, saveDetailPath)
    # 保存数据
    savePath = "快恋搜索结果-第" + str(current) + "页开始" + str(datetime.today().date()) + ".xls"
    saveKlData(dataList, savePath)


# 保存快恋搜搜数据
def saveKlData(dataList, savePath):
    print("saveing……saveKlData……")
    workBook = xlwt.Workbook(encoding="utf-8")
    workSheet = workBook.add_sheet("快恋搜索结果", cell_overwrite_ok=True)
    col = ("VipMID", "MLevel", "UserName", "Age", "Height")  # 列
    for i in range(0, 5):
        workSheet.write(0, i, col[i])  # 写入列名
    for i in range(0, len(dataList)):
        print("第%d条" % i)
        data = dataList[i]
        for j in range(0, 5):
            workSheet.write(i + 1, j, data[j])
    workBook.save(savePath)
    print("搜索页爬取完毕")


# 爬取数据
def getDataList(baseUrl, current, size):
    dataList = []
    # for i in range(1, 2288):  # 共计2288页数据范围
    # for i in range(1, 20):  # 共计2288页数据范围  一页20条数据，
    # for i in range(1, size):  # 共计2288页数据范围  一页20条数据，
    for i in range(current, size):  # 共计2288页数据范围  一页20条数据，
        # 1.获取数据源
        html = postTest(baseUrl, i)
        # 2.解析数据
        data = analyseKLData(html)
        for i in range(0, len(data)):
            dataList.append(data[i])
    return dataList


# 快恋网站解析数据
def analyseKLData(html):
    print("解析搜索页数据……")
    dataList = []
    object = json.loads(html)
    PageSize = object['PageSize']
    PageCount = object['PageCount']
    PageNo = object['PageNo']
    Amount = object['Amount']
    Entity = object['Entity']
    for i in range(0, len(Entity)):
        data = []
        VipMID = Entity[i]['VipMID']
        data.append(VipMID)
        MLevel = Entity[i]['MLevel']
        data.append(MLevel)
        UserName = Entity[i]['UserName']
        data.append(UserName)
        Age = Entity[i]['Age']
        data.append(Age)
        Height = Entity[i]['Height']
        data.append(Height)
        dataList.append(data)
    return dataList


# 得到页面内容
def postTest(url, current):
    print("请求搜索地址：" + url)
    from pip._vendor import requests
    # TODO-fwh-可以增加搜索条件
    d = {'PageNo': current,
         # 什么条件也不加  共计2289页
         # 'SCondition': '{\'Sex\':\'-1\',\'AgeB\':\'0\',\'AgeE\':\'0\',\'Edu\':\'0\',\'HeightB\':\'0\',\'HeightE\':\'0\',\'VipLevel\':\'-1\',\'Marry\':\'0\',\'House\':\'0\',\'Vocation\':\'不限\',\'XZ\':\'0\',\'SX\':\'0\',\'InCome\':\'0\',\'Car\':\'0\',\'HouseHoldP\':\'0\',\'HouseHoldC\':\'0\',\'Race\':\'0\',\'HeadPic\':\'1\',\'DSort\':\'1\'}'
         # 条件： 女 身高160起步  最大年龄33  共计519页
         'SCondition': '{\'Sex\':\'1\',\'AgeB\':\'0\',\'AgeE\':\'33\',\'Edu\':\'0\',\'HeightB\':\'160\',\'HeightE\':\'0\',\'VipLevel\':\'-1\',\'Marry\':\'0\',\'House\':\'0\',\'Vocation\':\'不限\',\'XZ\':\'0\',\'SX\':\'0\',\'InCome\':\'0\',\'Car\':\'0\',\'HouseHoldP\':\'0\',\'HouseHoldC\':\'0\',\'Race\':\'0\',\'HeadPic\':\'1\',\'DSort\':\'1\'}'
         # 条件：男 身高170起步  最大年龄40  共计889页
         # 'SCondition': '{\'Sex\':\'0\',\'AgeB\':\'0\',\'AgeE\':\'40\',\'Edu\':\'0\',\'HeightB\':\'170\',\'HeightE\':\'0\',\'VipLevel\':\'-1\',\'Marry\':\'0\',\'House\':\'0\',\'Vocation\':\'不限\',\'XZ\':\'0\',\'SX\':\'0\',\'InCome\':\'0\',\'Car\':\'0\',\'HouseHoldP\':\'0\',\'HouseHoldC\':\'0\',\'Race\':\'0\',\'HeadPic\':\'1\',\'DSort\':\'1\'}'
         }
    r = requests.post(url, data=d)
    # print(r.text)
    return r.text


# 测试调用程序
if __name__ == "__main__":
    for i in range(1, 7):
        size = i * 100
        current = size - 100
        a = datetime.now()
        # main(1, 100)  #第一页开始，循环100页
        # main(100, 200)  # 第100页开始，循环100页
        # main(200, 300)  # 第100页开始，循环100页
        # main(300, 400)  # 第100页开始，循环100页
        # main(400, 500)  # 第100页开始，循环100页
        # main(500, 600)  # 第100页开始，循环100页
        # main(1, 10)  # 第100页开始，循环100页
        main(current, size)
        # print(current,size)
        b = datetime.now()
        print("耗时：" + str((b - a).total_seconds()) + "秒")
